package models;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;

import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
@PrimaryKeyJoinColumn(name="id", referencedColumnName="id")
public class HitchhikerJourney extends Journey {

	public HitchhikerJourney(User user, String departurestate,
			String departuretownn, String departurestreet, Date departuredate,
			String departurehour, String arrivestate,
			String arrivetownn, String arrivestreet, String journeynotes, 
			boolean priceshare) {
		super(user, departurestate, departuretownn, departurestreet, 
				departuredate, departurehour, arrivestate, arrivetownn, arrivestreet, 
				journeynotes, priceshare);
		
	}
	
	
	public static List<HitchhikerJourney> getAllHitcJourney(){
		return HitchhikerJourney.findAll();	
	}
	
	public static List<HitchhikerJourney> orderHitchhikerJourney(){
		return (HitchhikerJourney.find("order by id desc").fetch());
	}
	
	public static List<HitchhikerJourney> searchHitchhikerJourney(String departurestate, String arrivestate) {

		if(departurestate != null) {
			departurestate = "%" + departurestate.toString() + "%";
			departurestate = departurestate.toLowerCase();
		}
		
		if(arrivestate != null) {
			arrivestate = "%" + arrivestate.toString() + "%";
			arrivestate = arrivestate.toLowerCase();
		}

		return (HitchhikerJourney.find("LOWER(departurestate) like ? AND LOWER(arrivestate) like ? order by id desc", departurestate, arrivestate).fetch());
	}
	
	public static List<HitchhikerJourney> filterHitchhikerJourney(String departurestate, String departuretownn,	String departurestreet, String arrivestate, String arrivetownn, String arrivestreet) {
		if(departurestate != null) {
			departurestate = "%" + departurestate.toString() + "%";
			departurestate = departurestate.toLowerCase();
		}
		
		if(departuretownn != null) {
			departuretownn = "%" + departuretownn.toString() + "%";
			departuretownn = departuretownn.toLowerCase();
		}
		
		if(departurestreet != null) {
			departurestreet = "%" + departurestreet.toString() + "%";
			departurestreet = departurestreet.toLowerCase();
		}
		
		if(arrivestate != null) {
			arrivestate = "%" + arrivestate.toString() + "%";
			arrivestate = arrivestate.toLowerCase();
		}
		
		if(arrivetownn != null) {
			arrivetownn = "%" + arrivetownn.toString() + "%";
			arrivetownn = arrivetownn.toLowerCase();
		}
		
		if(arrivestreet != null) {
			arrivestreet = "%" + arrivestreet.toString() + "%";
			arrivestreet = arrivestreet.toLowerCase();
		}

		return (HitchhikerJourney.find("LOWER(departurestate) like ? AND LOWER(departuretownn) like ? AND LOWER(departurestreet) like ? AND LOWER(arrivestate) like ? AND LOWER(arrivetownn) like ? AND LOWER(arrivestreet) like ? order by id desc", departurestate, departuretownn, departurestreet, arrivestate, arrivetownn, arrivestreet).fetch());
	}
	
	public void updateHitc(String departurestate, String departuretownn, String departurestreet, Date departuredate,
			String departurehour, String arrivestate, String arrivetownn, String arrivestreet, String journeynotes, boolean priceshare){
		
		this.setDeparturestate(departurestate);
		this.setDeparturetownn(departuretownn);
		this.setDeparturestreet(departurestreet);
		this.setDeparturedate(departuredate);
		this.setDeparturehour(departurehour);
		this.setArrivestate(arrivestate);
		this.setArrivetownn(arrivetownn);
		this.setArrivestreet(arrivestreet);
		this.setJourneynotes(journeynotes);
		this.setPriceshare(priceshare);
		this.save();
		
	}
	
}
